<script setup lang="ts">
  import { NButton, NSpin } from 'naive-ui'
  import { useRoute } from 'vue-router'
  import { auditScheme, downloadAuditReport, getSchemeCategoryDetail } from '@/api/scheme'
  import WordPreview from '@/components/WordPreview/index.vue'
  import { onMounted, ref } from 'vue'
  import BackNav from '@/components/common/BackNav/index.vue'
  import { Typewriter } from 'vue-element-plus-x'
  import { generateMarkdown } from '@/utils/functions'
  import { createChat } from '@/api/agent'
  import { getContent } from '../Build/utils'
  import { useAgent } from '@/hooks/useAgent'
  const route = useRoute()
  const id = route.params.id as string
  const { userName } = useAgent()
  onMounted(async () => {
    await init()
  })

  const content = ref()
  const isLoading = ref(false)
  const init = async () => {
    const res = await getSchemeCategoryDetail({ id })
    console.log(res)
    if (res) {
      file.value = res.schemeUrl
      if (res.auditRecordDtos.length > 0) {
        const { content: answerContent } = getContent(res.auditRecordDtos[0].auditResult)
        content.value = answerContent
      }
    }
  }
  const file = ref()

  const handleAudit = async () => {
    isLoading.value = true
    const res = await createChat({
      query: '帮我检修一个方案',
      userName,
      responseMode: 'blocking',
      inputs: {
        file_path: file.value,
      },
    }).finally(() => {
      isLoading.value = false
    })
    const { content: answerContent } = getContent(res.answer)
    content.value = answerContent
    await auditScheme({
      schemeAuditId: id,
      auditStatus: 1,
      type: 2,
      auditResult: res.answer,
    })
  }

  const wordPreviewId = ref(`${new Date().getTime()}`)
  const handleDownload = async () => {
    const res = await downloadAuditReport({ id })
    const { content: answerContent } = getContent(res)
    generateMarkdown(answerContent, '审核报告.md')
  }
</script>
<template>
  <div class="h-full flex py-5 gap-4 pingfang result-page">
    <div class="h-full bg-white rounded flex-1 flex flex-col gap-4">
      <BackNav class="border-b border-b-[#F0F0F0]" title="原文概览" />
      <div class="flex-1">
        <div class="h-full bg-white rounded p-5">
          <WordPreview
            v-if="file"
            mode="view"
            :file="file"
            :id="wordPreviewId"
            :userName="userName"
          />
        </div>
      </div>
    </div>
    <n-spin class="h-full" :show="isLoading">
      <template #description> 重新审核中... </template>
      <div class="h-full bg-white rounded w-[724px]">
        <div class="h-[58px] leading-[58px] px-5 flex items-center justify-between">
          <div class="font-bold text-[20px]">审核结果</div>
          <div class="flex items-center gap-2">
            <n-button @click="handleAudit" type="primary" class="flex items-center">
              重新审核
            </n-button>
            <n-button @click="handleDownload"> 下载报告 </n-button>
          </div>
        </div>
        <div class="h-[calc(100%-58px)] overflow-y-auto">
          <div class="h-full bg-white rounded px-5">
            <Typewriter :content="content" is-markdown />
          </div>
        </div>
      </div>
    </n-spin>
  </div>
</template>
7

<style scoped lang="less">
  :deep(strong) {
    font-size: 17px;
  }

  :deep(.n-spin-content) {
    height: 100%;
  }
</style>
